﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Menu
{
    public partial class PartyModuleNavigationEdit : PageBase
    {
        #region ViewPower
        public string id { get { return GetQueryIntValue("id"); } }
        public string module { get { return GetQueryIntValue("module"); } }
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyModuleNavigationAdd";
            }
        }

        #endregion

        #region Page_Load

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBind(ddlModule,
                DB.PartyModuleNavigations.Where(a => string.IsNullOrEmpty(a.Category)), "PageName", "ID", true);
        }

        private void LoadData()
        {

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyModuleNavigation current = DB.PartyModuleNavigations.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
               
                //txtCategory.Readonly = true;
                txtCategory.Text = current.Category;
                ddlModule.Text = current.Module; ;
                txtPageName.Text = current.PageName;
                txtUrl.Text = current.Url;
                ddlModule.SelectedValue = current.Module;
                txtOrderNo.Text = current.OrderNo.ToString();
                cbxEnabled.Checked = current.Enabled;
                ddlTarget.SelectedValue = current.TargetType;
            }
            else
            {
                ddlModule.SelectedValue = module;
            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {
            PartyModuleNavigation item;
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                item = DB.PartyModuleNavigations.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
            }
            else
            {

                item = new PartyModuleNavigation();
                item.ID = StringHelper.NewID;
                DB.PartyModuleNavigations.Add(item);
            }
            item.Category = txtCategory.Text;
            item.Module = ddlModule.SelectedValue;
            item.PageName = txtPageName.Text;
            item.Url = txtUrl.Text;
            item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            item.Enabled = cbxEnabled.Checked;
            item.TargetType= ddlTarget.SelectedValue;
            DB.SaveChanges();
            PartyModuleNavigationCaching.Clean();
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}

